/**
 *
 * Copyright 2012 TeleNav, Inc. All rights reserved.
 * ApacheUtility.java
 * @author jwmeng (jwmeng@telenavsoftware.com)
 * @date 2012-10-19
 */
package com.telenav.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

public class ApacheUtility {

	public static InputStream GetImageByUrl(String uri)
			throws MalformedURLException {
		URL url = new URL(uri);
		URLConnection conn;
		InputStream is;
		try {
			conn = url.openConnection();
			conn.connect();
			is = conn.getInputStream();


			// is=(InputStream)url.getContent();
			return is;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static Bitmap GetBitmapByUrl(String uri) {

		Bitmap bitmap = null;
		InputStream is;
		try {

			is = GetImageByUrl(uri);
			if(is != null)
			{
				bitmap = BitmapFactory.decodeStream(is);
				is.close();
			}
			return bitmap;

		} catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static Drawable GetDrawableByUrl(String uri) {

		Drawable drawable;
		InputStream is;
		try {

			is = GetImageByUrl(uri);

			drawable = Drawable.createFromStream(is, "src");

			is.close();

			return drawable;

		} catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

}